package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.SysMenu;
import com.easy.common.service.IMenuService;

import com.easy.user.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuService extends ServiceImpl<MenuMapper,SysMenu> implements IMenuService {

    @Autowired
    MenuMapper sysMenuMapper;

    @Override
    public SysMenu insert(SysMenu sysMenu) {
        super.save(sysMenu);
        int id = sysMenu.getId();//因为他在Bean类是自增长的，所以可以直接获取
        SysMenu result = super.getById(id);
        return result;
    }

    @Override
    public SysMenu update(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
        return sysMenuMapper.selectById(sysMenu.getId());
    }

    @Override
    public int delete(Integer id) {
        return sysMenuMapper.deleteById(id);
    }

    @Override
    public SysMenu getById(Integer id) {
        return sysMenuMapper.selectById(id);
    }

    @Override
    public IPage<SysMenu> getListByPage(IPage page, SysMenu sysMenu) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>(sysMenu);
        return sysMenuMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysMenu> getList(SysMenu sysMenu) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>(sysMenu);
        return sysMenuMapper.selectList(queryWrapper);
    }

    @Override
    public int setVisible(Integer id) {
        SysMenu menu = sysMenuMapper.selectById(id);
        if (menu != null) {
            int currentVisibility = menu.getVisible();
            menu.setVisible(currentVisibility == 1 ? 0 : 1);
            return sysMenuMapper.updateById(menu);
        }
        return 0;
    }
}